package com.hengxinguotong.hxgtwygsp.ui.main.picture;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.Rule;
import com.hengxinguotong.hxgtwygsp.model.WeatherBean;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseFragment;
import com.hengxinguotong.hxgtwygsp.ui.main.RuleNoticeAdapter;
import com.hengxinguotong.hxgtwygsp.ui.readFile.FileDisplayActivity;
import com.hengxinguotong.hxgtwygsp.ui.regulation.NoticeAndRuleData;
import com.hengxinguotong.hxgtwygsp.ui.regulation.RegulationActivity;
import com.hengxinguotong.hxgtwygsp.utils.CommUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class PictureFragment extends BaseFragment implements PictureContract.IView {

    @Inject
    PicturePresenter mPresenter;

    @BindView(R.id.ll_public_notice)
    LinearLayout mLlPublicNotice;
    @BindView(R.id.rv_public)
    RecyclerView mRvPublic;
    @BindView(R.id.ll_important_notice)
    LinearLayout mLlImportantNotice;
    @BindView(R.id.rv_important)
    RecyclerView mRvImportant;
    @BindView(R.id.ll_sweet_remind)
    LinearLayout mLlSweetRemind;
    @BindView(R.id.main_date)
    TextClock mMainDate;
    @BindView(R.id.main_time)
    TextClock mMainTime;
    @BindView(R.id.ll_date)
    LinearLayout mLlDate;
    @BindView(R.id.tv_weather)
    TextView mTvWeather;
    @BindView(R.id.tv_temperature)
    TextView mTvTemperature;
    @BindView(R.id.tv_temperature_range)
    TextView mTvTemperatureRange;
    @BindView(R.id.tv_air_quality)
    TextView mTvAirQuality;
    @BindView(R.id.tv_wind)
    TextView mTvWind;
    @BindView(R.id.tv_humidity)
    TextView mTvHumidity;
    @BindView(R.id.tv_pressure)
    TextView mTvPressure;

    public RuleNoticeAdapter mPublicAdapter, mImportantAdapter;
    public ArrayList<Rule> mPublicList, mImportantList;

    private boolean isInit;//是否初始化

    @Inject
    public PictureFragment() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPresenter.bindView(this);
        isInit = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.dropView();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_picture;
    }

    @Override
    public void init() {
        mPublicList = NoticeAndRuleData.getPublicHeadList();
        mPublicAdapter = new RuleNoticeAdapter(R.layout.item_rule_notice,mPublicList);
        mRvPublic.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvPublic.setAdapter(mPublicAdapter);

        mImportantList = NoticeAndRuleData.getImportantHeadList();
        mImportantAdapter = new RuleNoticeAdapter(R.layout.item_rule_notice,mImportantList);
        mRvImportant.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvImportant.setAdapter(mImportantAdapter);

        mPublicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Rule rule = mPublicList.get(position);
                FileDisplayActivity.show(getContext(), rule.getTitle(), CommUtil.getDataDir(getContext()) + File.separator + rule.getFileName());
            }
        });

        mImportantAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Rule rule = mImportantList.get(position);
                String path = CommUtil.getDataDir(getContext()) + File.separator + rule.getFileName();
                Log.e("test", "onItemClick: "+path);
                FileDisplayActivity.show(getContext(), rule.getTitle(), CommUtil.getDataDir(getContext()) + File.separator + rule.getFileName());
            }
        });
        //获取天气
        mPresenter.getWeather();
    }

    @OnClick({R.id.ll_public_notice, R.id.ll_important_notice})
    public void onClickTab(View v) {
        switch (v.getId()) {
            case R.id.ll_public_notice:
                Intent intent = new Intent(getContext(), RegulationActivity.class);
                intent.putExtra("position",0);
                startActivity(intent);
                break;
            case R.id.ll_important_notice:
                intent = new Intent(getContext(), RegulationActivity.class);
                intent.putExtra("position",1);
                startActivity(intent);
                break;
        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            if (isInit) {
                //获取天气
                mPresenter.getWeather();
            }
        } else {
        }
    }


    @Override
    public void updateHanderStatus() {
    }

    @Override
    public void updateCompleted() {
    }

    @Override
    public void getWeatherSucc(WeatherBean weatherBean) {
        if (weatherBean != null){
            WeatherBean.DataBean data = weatherBean.getData();
            if(data != null){
                List<WeatherBean.ForecastBean> forecast = data.getForecast();
                if (forecast != null && forecast.size()>0){
                    WeatherBean.ForecastBean bean = forecast.get(0);
                    //设置天气
                    mTvWeather.setText(bean.getType());
                    mTvAirQuality.setText("空气质量："+data.getQuality());
                    mTvTemperature.setText(data.getWendu()+"℃");
                    mTvTemperatureRange.setText(bean.getLow()+"/"+bean.getHigh());
                    mTvWind.setText(bean.getFx()+":"+bean.getFl());
                    mTvHumidity.setText("湿度："+data.getShidu());
                    mTvPressure.setText(bean.getNotice());
                }
            }
        }
    }


}
